﻿using System.Linq;
using MediatR;
using ProduceBasicData.Api.Application.Command.Workstation;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.WorkStation
{
    /// <summary>
    /// 工位管理展示处理类
    /// </summary>
    public class GetWorkStationHandler : IRequestHandler<GetWorkStationCommand, ApiPaging<WorkStationDto>>
    {
        private readonly IBaseRespository<WorkstationModel> stationRepo;
        private readonly IBaseRespository<ProductionModel> produRepo;
        private readonly IBaseRespository<ProcessModel> processRepo;

        public GetWorkStationHandler(IBaseRespository<WorkstationModel> stationRepo,
            IBaseRespository<ProductionModel> produRepo,
            IBaseRespository<ProcessModel> processRepo)
        {
            this.stationRepo = stationRepo;
            this.produRepo = produRepo;
            this.processRepo = processRepo;
        }

        public Task<ApiPaging<WorkStationDto>> Handle(GetWorkStationCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<WorkStationDto> res=new ApiPaging<WorkStationDto>();
            var station = stationRepo.GetAll().Where(x=>!x.Isdel);
            var produ = produRepo.GetAll().Where(x => !x.Isdel);
            var process = processRepo.GetAll().Where(x => !x.Isdel);

            //编码
            if (!string.IsNullOrEmpty(request.Workstationcode))
            {
                station = station.Where(x=>x.Workstationcode.Contains(request.Workstationcode));
            }

            //名称
            if (!string.IsNullOrEmpty(request.WorkstationName))
            {
                station = station.Where(x => x.WorkstationName.Contains(request.WorkstationName));
            }

            //产线
            if (request.Productionid!=null)
            {
                station = station.Where(x => x.Productionid==request.Productionid);
            }

            //工序
            if (request.Processid != null)
            {
                station = station.Where(x => x.Processid==request.Processid);
            }

            //描述
            if (!string.IsNullOrEmpty(request.Workstationdesc))
            {
                station = station.Where(x => x.Workstationdesc.Contains(request.Workstationdesc));
            }

            //创建人
            if (!string.IsNullOrEmpty(request.CreateName))
            {
                station = station.Where(x => x.CreateName.Contains(request.CreateName));
            }

            //创建时间
            if (request.Createkstime != null && request.Createjstime != null)
            {
                station = station.Where(x => x.CreateTime.AddDays(1) >= request.Createkstime && x.CreateTime <= request.Createjstime);
            }

            //更新时间
            if (request.UpdateksTime != null && request.UpdatejsTime != null)
            {
                station = station.Where(x => x.UpdateTime >= request.UpdateksTime && x.UpdateTime <= request.UpdatejsTime);
            }

            var linq = from a in station
                       join b in produ on a.Productionid equals b.Id
                       join c in process on a.Processid equals c.Id
                       select new WorkStationDto
                       {
                           Id = a.Id,
                           WorkstationName=a.WorkstationName,
                           Processid=a.Processid,
                           Processname=c.Processname,
                           Productionid=a.Productionid,
                           Productionname=b.Productionname,
                           Workstationcode=a.Workstationcode,
                           Workstationdesc=a.Workstationdesc,
                           CreateName=a.CreateName,
                           CreateTime=a.CreateTime,
                           UpdateName= a.UpdateName,
                           UpdateTime=a.UpdateTime,
                       };
            res.PageDatas=linq.OrderByDescending(x=>x.CreateTime).Skip((request.Index-1)*request.Size).Take(request.Size).ToList();
            res.TotalCount = linq.Count();
            res.Code=ApiEnum.Success;
            res.Msg = "工位信息查询成功";
            return Task.FromResult(res);
        }
    }
}
